<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\ToolAction\ReturnActions;
use App\Admin\Actions\Post\Restore;
use App\Admin\Extensions\Excel\MedicalExporter;
use App\Admin\Repositories\Medical;
use App\Models\Medical as ModelsMedical;
use App\Models\MemberDoctor;
use App\Models\User;
use Dcat\Admin\Grid\Tools;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class MemberMedicalController extends AdminController
{

    public function title()
    {
        return '专家明细';
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Medical(['contract']), function (Grid $grid) {
            // $grid->disableViewButton();
            $grid->disableCreateButton();

            $grid->disableActions();
            // 横向拖动
            $grid->scrollbarX();
            // 表格边框
            $grid->withBorder();
            // 文字居中
            $grid->addTableClass(['table-text-center']);

            $user_id = request()->input('user_id');
            if (!empty($user_id) || isset($user_id)) {
                $grid->model()->where('user_id', $user_id);
            }

            if (!Admin::user()->isAdministrator()) {
                /**
                 * 工具按钮.
                 */
                $grid->tools(function (Tools $tools) {
                    $tools->append(new ReturnActions());
                });
            }



            $grid->export(new MedicalExporter());
            $grid->column('id')->sortable();
            // $grid->column('title')->limit(10);
            // $grid->column('act_id', '所属专题')->display(function ($id) {
            //     return Activity::find($id)->activity_name ?? '';
            // })->label();

            // $grid->column('user_id', '真实姓名')->display(function ($userid) {
            //     return MemberDoctor::where('user_id', $userid)->first()->real_name ?? '';
            // });


            // $grid->column('代理人')->display(function () {
            //     return MemberDoctor::where('user_id', $this->user_id)->first()->agent ?? '';
            // });
            $grid->column('atm_coding', '病例编码')->copyable();
            $grid->column('invoice_img', '发票图片')->display(function ($pictures) {
                return explode(',', $pictures);
            })->image('',50,50);

            $grid->column('discharge_summary_img', '出院小结图片')->display(function ($pictures) {
                return explode(',', $pictures);
            })->image('',50,50);
            // if (Admin::user()->can('medical-audit')) {
            //     $grid->status('状态')->radio(['未审核', '已审核', '驳回']);
            // }

            // $grid->is_show('前台是否展示')->radio(['隐藏', '展示']);
            // $grid->column('is_hot', '热门推荐')->switch();

            $grid->column('created_at', '病例发布时间');

            $grid->status('病例状态')->using([0 => '待审核', 1 => '已审核', 2 => '已驳回'])->badge([
                0 => 'primary',
                1 => 'success',
                2 => 'danger',
            ]);
            $grid->column('examine_time', '病例审核时间')->display(function ($examine_time) {
                if (empty($examine_time)) {
                    return '——';
                } else {
                    return $examine_time;
                }
            });
            $grid->column('contract.audit_status', '劳务协议状态')->using([0 => '待审核', 1 => '已审核', 2 => '已驳回'])->badge([
                0 => 'primary',
                1 => 'success',
                2 => 'danger',
            ]);;
            $grid->column('contract.audit_time', '劳务协议审核时间')->display(function ($auditTime) {

                if (empty($auditTime)) {
                    return '——';
                } else {
                    return $auditTime;
                }
            });
            $grid->column('contract.is_pay', '劳务支付状态')->using([0 => '待支付', 1 => '已支付'])->badge([
                0 => 'primary',
                1 => 'success',
                // 2 => 'danger',, 2 => '已驳回'
            ]);;
            $grid->column('contract.pay_time', '劳务支付时间')->display(function ($pay_time) {

                if (empty($pay_time)) {
                    return '——';
                } else {
                    return $pay_time;
                }
            });
            $grid->model()->orderBy('id', 'desc');
            $grid->model()->where('is_del', 0);

            /**
             * 按钮控制.
             */
            // @permissions
            if (!Admin::user()->can('medical-create')) {
                $grid->disableCreateButton();
                // $grid->disableQuickCreateButton();
            }
            // @permissions
            if (!Admin::user()->can('medical-update')) {
                $grid->disableEditButton();
                // $grid->disableQuickEditButton();
            }
            // @permissions
            if (!Admin::user()->can('medical-delete')) {
                $grid->disableDeleteButton();
            }

            $grid->filter(function (Grid\Filter $filter) {
                // $filter->equal('id');
                $filter->equal('atm_coding', '编码');
                // $filter->like('memberDoctor.agent', '代理人');
                $filter->equal('status', '病例状态')->select([0 => '待审核', 1 => '已审核', 2 => '待审核']);
                $filter->equal('contract.audit_status', '劳务协议状态')->select([0 => '未审核', 1 => '已审核', 2 => '待审核']);
                $filter->equal('contract.is_pay', '劳务支付状态')->select([0 => '待支付', 1 => '已支付']);
                // $filter->equal('user_id', '医生名称')->select(MemberDoctor::all()->pluck('real_name', 'user_id'));
                // $filter->scope('trashed', '回收站')->onlyTrashed();
            });
            // $grid->selector(function (Grid\Tools\Selector $selector) {
            //     $selector->select('status', '状态', [0 => '未审核', 1 => '已审核', 2 => '驳回']);
            // });
            // $grid->actions(function (Grid\Displayers\Actions $actions) {
            //     if (request('_scope_') == 'trashed') {
            //         // @permissions
            //         if (!Admin::user()->can('medical-delete')) {
            //             $actions->disableDelete();
            //         }
            //         $actions->disableEdit();
            //         // $actions->disableView();
            //         $actions->append(new Restore(ModelsMedical::class));
            //     }
            // });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Medical(), function (Show $show) {
            // @permissions
            if (!Admin::user()->can('medical-delete')) {
                $show->disableDeleteButton();
            }

            // @permissions
            if (!Admin::user()->can('medical-update') || (Admin::user()->id != $show->model()->admin_id) && !Admin::user()->isAdministrator()) {
                $show->disableEditButton();
            }

            $show->field('id');
            // $show->field('title');
            // $show->field('act_id', '活动专题')->as(function ($id) {
            //     return Activity::find($id)->activity_name ?? '未关联共识';
            // })->label();
            // $show->field('medical_history')->unescape();
            // $show->field('symptoms')->unescape();
            // $show->field('diagnosis')->unescape();
            // $show->field('treatment')->unescape();
            // $show->field('detail')->unescape();

            // $show->field('cate_id')->as(function ($c_id) {
            //     return Category::find($c_id)->title ?? '';
            // })->label();
            $show->field('atm_coding', '编码');
            // $show->field('user_id', '用户名')->as(function ($user_id) {
            //     $member = Member::find($user_id);
            //     return $member->username . '  /  (' . $member->realname . ' )';
            // })->label();
            // $show->medical_data()->file();
            $show->invoice_img('发票图片')->image();
            $show->discharge_summary_img('出院小结图片')->image();
            // $show->field('created_at');
            // $show->field('updated_at');
            // $show->field('examine_time');
            // $show->field('reason');
            // $show->status()->using(['1' => '显示', '0' => '不显示']);
            // $show->is_del()->using(['1' => '已删除', '0' => '未删除']);
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Medical(), function (Form $form) {
            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->disableViewButton();
            // @permissions
            if (!Admin::user()->can('medical-delete')) {
                $form->disableDeleteButton();
            }
            $form->display('id');
            // $form->text('title')->required();
            // $form->select('act_id', '所属专题')->options(Activity::where('is_del', 0)->where('status', 1)->pluck('activity_name', 'id'))->saving(function ($value) {
            //     return $value;
            // })->required()->label();

            // if (empty($form->model()->act_id)) {
            // $form->select('cate_id')->options(Category::where('parent_id', 1)->pluck('title', 'id'))->saving(function ($value) {
            // return $value;
            // })->required()->label();
            // $form->select('area_id', '赛区选择')->options(ActivityAreaSetting::selectOptions())->saving(function ($value) {
            // return $value;
            // })->required()->label();
            // } else {
            // $category_ids = Activity::find($form->model()->act_id)->category_ids ?? '';
            // $form->select('cate_id')->options(Category::whereIn('id', explode(',', $category_ids))->where('parent_id', 1)->pluck('title', 'id'))->saving(function ($value) {
            //     return $value;
            // })->required()->label();

            // $array = ActivityAreaSetting::where('act_id', $form->model()->act_id)->get();
            // $array->makeHidden('updated_at');
            // $array = $array->toArray();
            // $trees = TreeService::toListByKey($array);

            // $form->select('area_id', '赛区选择')->options($trees)->saving(function ($value) {
            //     return $value;
            // })->required()->label();
            // }


            // $form->image('image', '封面图')->move('Medical/image/' . date('Y/m'));

            // $form->textarea('desc', '简介')->disable();
            // $form->editor('medical_history');
            // $form->editor('symptoms');
            // $form->editor('diagnosis');
            // $form->editor('treatment');
            // $form->editor('detail');

            $form->text('atm_coding', '编码')->required();
            $form->multipleFile('invoice_img', '发票图片')->move('medical/file/' . date('Y/m/d') . '/' . uniqid())->saveFullUrl()->saving(function ($paths) {

                return implode(',', $paths);
            })->downloadable();
            $form->multipleFile('discharge_summary_img', '出院小结图片')->move('medical/file/' . date('Y/m/d') . '/' . uniqid())->saveFullUrl()->saving(function ($paths) {

                return implode(',', $paths);
            })->downloadable();

            // $form->text('author');
            //   $form->display('user_id', '用户名称')->with(function ($value) {
            //     $member = Member::find($value);
            //     if(!empty($member)){
            //         $realname = isset($member->realname) ? $member->realname : '未填写';
            //         return $member->username . ' - ' . $realname;
            //     }else{
            //         return '未找到用户';
            //     }

            // });

            $form->hidden('status')
                // ->customFormat(function ($v) {
                //     return $v == 1 ? 1 : 0;
                // })
                ->saving(function ($v) {
                    return $v;
                });
            $form->hidden('is_show')
                // ->customFormat(function ($v) {
                //     return $v == 1 ? 1 : 0;
                // })
                ->saving(function ($v) {
                    return $v;
                });

            $form->hidden('is_hot')
                // ->customFormat(function ($v) {
                //     return $v == 1 ? 1 : 0;
                // })
                ->saving(function ($v) {
                    return $v;
                });
            $form->radio('is_show', '是否在前台显示')->options([0 => '隐藏', 1 => '展示'])->default(1);
            if (Admin::user()->can('medical-audit') || Admin::user()->isAdministrator()) {
                $form->radio('status', '状态')->when([1, 2], function (Form $form) {
                    $form->textarea('reason', '意见');

                    $form->hidden('examine_time');
                    $form->saving(function (Form $form) {
                        $form->examine_time = date('Y-m-d H:i:s', time());
                    });
                })->options([0 => '未审核', 1 => '已审核', 2 => '驳回'])->default(0);
            }
        });
    }
}
